package com.miplus.ccrm.common;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JUsrPerm {
    public static String DISSTAR_PHN="disstar.phone";
    public static String DISSTAR_IDNO="disstar.idno";
    public static String DISSTAR_CARD="disstar.cardno";
    public static String DISSTAR_ADDRESS="disstar.address";

    public static String VIEW_ALLORGIN="view.allorgin";
    public static String VIEW_ALLFICASE="view.allficase";
    public static String VIEW_GRPFICASE="view.grpficase";

    public static boolean hasPermission(JSysUser user,String perm)
    {
        if(user==null) return false;
        if(JPubUtils.IsEmpty(user.getPermlist())){
            return false;
        }
        for(String p : user.getPermlist()){
            if(JPubUtils.IsEqual(p,perm)){
                return true;
            }
        }
        return false;
    }

    public static String getPermission(JSysUser user,String perm)
    {
        if(user==null) return "";
        if(JPubUtils.IsEmpty(user.getPermlist())){
            return "";
        }
        for(String p : user.getPermlist()){
            if(JPubUtils.IsEqual(p,perm)){
                return perm;
            }
        }
        return "";
    }

    public static boolean viewAllFiCase(JSysUser user)
    {
        if(user==null) return false;

        if(JPubUtils.IsEmpty(user.getPermlist())){
            return false;
        }
        for(String p : user.getPermlist()){
            if(JPubUtils.IsEqual(p,VIEW_ALLFICASE)){
                return true;
            }
        }
        return false;
    }
    public static boolean viewGrpFiCase(JSysUser user)
    {
        if(user==null) return false;

        if(JPubUtils.IsEmpty(user.getPermlist())){
            return false;
        }
        for(String p : user.getPermlist()){
            if(JPubUtils.IsEqual(p,VIEW_GRPFICASE)){
                return true;
            }
        }
        return false;
    }
    public static String apiToPermission(String apiname){

        log.info("apiToPermission:"+apiname);

        /**
         * 控制逻辑:
         * 1.根据API名称，找到对应的权限，
         * 2.如果权限为空，则返回空，
         * 3.如果权限不为空，则返回权限，
         * 4.如果权限为空，则返回空
         */
        if(JPubUtils.IsEqual(apiname,"campaignList")||
                JPubUtils.IsEqual(apiname,"campaignInsert")||
                JPubUtils.IsEqual(apiname,"campaignUpdate")||
                JPubUtils.IsEqual(apiname,"campaignDelete")) {
            return "csactivity";
        }
        else if(JPubUtils.IsEqual(apiname,"triggerInsert")||
                JPubUtils.IsEqual(apiname,"triggerUpdate")||
                JPubUtils.IsEqual(apiname,"triggerDelete")||
                JPubUtils.IsEqual(apiname,"cfgActivityInsert")||
                JPubUtils.IsEqual(apiname,"cfgActivityUpdate")||
                JPubUtils.IsEqual(apiname,"cfgActivityDelete")){
            return "trigger";
        }
        else if(JPubUtils.IsEqual(apiname,"strategyList")||
                JPubUtils.IsEqual(apiname,"strategyInsert")||
                JPubUtils.IsEqual(apiname,"strategyUpdate")||
                JPubUtils.IsEqual(apiname,"strategyDelete")){
            return "strategy";
        }
        else if(JPubUtils.IsEqual(apiname,"addRuleTask")||
                JPubUtils.IsEqual(apiname,"updateRuleTask")||
                JPubUtils.IsEqual(apiname,"deleteRuleTask")||
                JPubUtils.IsEqual(apiname,"getRuleTaskList")||
                JPubUtils.IsEqual(apiname,"getRuleTaskInfo")||
                JPubUtils.IsEqual(apiname,"addRuleStep")||
                JPubUtils.IsEqual(apiname,"deleteRuleStep")||
                JPubUtils.IsEqual(apiname,"updateRuleStep")
        ){
            return "task";
        }
        else if(JPubUtils.IsEqual(apiname,"anouceInsert")||
                JPubUtils.IsEqual(apiname,"annouceUpdate")||
                JPubUtils.IsEqual(apiname,"annouceDelete")||
                JPubUtils.IsEqual(apiname,"annouceList")
        ){
            return "NoticeSend";//公告发布
        }else if(JPubUtils.IsEqual(apiname,"annouceViewList")
        ){
            return "AnnouncementList";//公告列表
        }else if(JPubUtils.IsEqual(apiname,"roleList")||
                JPubUtils.IsEqual(apiname,"updateRole")||
                JPubUtils.IsEqual(apiname,"insertRole")||
                JPubUtils.IsEqual(apiname,"updateRoleStatus")||
                JPubUtils.IsEqual(apiname,"deleteRole")
        ){
            return "rolelist";//角色管理
        }

        return "";
    }
}
